#include "DataCache.h"
#include <assert.h>
#include <memory>
#include <string.h>

DataCache::DataCache(int capacity):
    m_capacity(capacity),
    m_data_size(0)
{
    assert(capacity > 0);
    m_data = new char[capacity];
}

DataCache::DataCache() :
    m_data(NULL),
    m_capacity(0),
    m_data_size(0)
{
}

DataCache::DataCache(DataCache&& other)
{
    this->m_data = other.m_data;
    this->m_data_size = other.m_data_size;
    this->m_capacity = other.m_capacity;
    this->m_in_index = other.m_in_index;

    other.m_data = NULL;
    other.m_data_size = 0;
}

DataCache& DataCache::operator=(DataCache&& other)
{
    if (this == &other) return *this;

    if (this->m_data) delete[] this->m_data;

    this->m_data = other.m_data;
    this->m_data_size = other.m_data_size;
    this->m_capacity = other.m_capacity;
    this->m_in_index = other.m_in_index;

    other.m_data = NULL;
    other.m_data_size = 0;

    return *this;
}

const char DataCache::operator[](int index) const
{
    if (index >= m_data_size) return ' ';
    return *(m_data + index);
}

char& DataCache::operator[](int index)
{
    if (index >= m_data_size) return *(m_data + m_capacity - 1);
    return *(m_data + index);
}

char* DataCache::operator()()
{
    return m_data;
}

const char* DataCache::operator()() const
{
    return m_data;
}

DataCache::~DataCache()
{
    if (m_data) delete[] m_data;
    m_data = NULL;
}